package cn.ekuma.epos.order;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.swing.JButton;

import com.openbravo.bean.Location;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;

import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicReceipts;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;

public abstract class AbstractJOrderProcessPanel extends JPanel {
	protected JOrderView srcOrderView,distOrderView;
	private JSplitPane splitPane;
	private JPanel commandPanel;
	private I_DataLogicReceipts dlReceipts;
	protected AppView app;
	private I_DataLogicERP dlSales;
	private I_DataLogicSystem dlSystem;
	I_DataLogicCustomers dlCustomers;
	private JButton button;
	private JSeparator separator;
	private JButton btnNewButton_1;
	private JButton button_1;
	/**
	 * Create the panel.
	 */
	public AbstractJOrderProcessPanel(AppView app,I_DataLogicReceipts dlReceipts,I_DataLogicERP dlSales,I_DataLogicSystem dlSystem,I_DataLogicCustomers dlCustomers) {
		initComponents();
		this.dlReceipts=dlReceipts;
		this.dlSales = dlSales;
		this.dlSystem=dlSystem;
		this.dlCustomers=dlCustomers;
		List<Location> locations;
		try {
			locations = dlSales.list(Location.class);
		} catch (BasicException ex) {
			new MessageInf(ex).show(this);
			locations = new ArrayList();
		}
		srcOrderView=new JOrderView(locations,dlSystem);
		splitPane.setLeftComponent(srcOrderView);
		distOrderView=new JOrderView(locations,dlSystem);
		splitPane.setRightComponent(srcOrderView);
	}
	
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		add(splitPane, BorderLayout.CENTER);
		
		commandPanel = new JPanel();
		add(commandPanel, BorderLayout.EAST);
		commandPanel.setLayout(new GridLayout(15, 1, 0, 0));
		
		JButton btnNewButton = new JButton("\u9009\u62E9\u5355\u636E");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				m_jListOrdersActionPerformed(e);
			}
		});
		commandPanel.add(btnNewButton);
		
		button = new JButton("\u4FDD\u5B58\u5355\u636E");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				m_jSaveOrdersActionPerformed(e);
			}
		});
		commandPanel.add(button);
		
		button_1 = new JButton("\u4E0A\u79FB\u5355\u636E");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				m_jUpOrdersActionPerformed(e);
			}
		});
		commandPanel.add(button_1);
		
		separator = new JSeparator();
		commandPanel.add(separator);
		
		btnNewButton_1 = new JButton("New button");
		commandPanel.add(btnNewButton_1);
	}
	
	protected void m_jUpOrdersActionPerformed(ActionEvent e) {
		if(distOrderView.getActiveOrder()!=null)
			srcOrderView.setActiveOrder(distOrderView.getActiveOrder());
	}

	protected void m_jSaveOrdersActionPerformed(ActionEvent e) {
		if(distOrderView.getActiveOrder()!=null)
			if(distOrderView.getActiveOrder().getId()!=null)
				saveCurrentOrder(distOrderView.getActiveOrder());
			else 
				insertShardOrder(distOrderView.getActiveOrder());
	}

	public abstract void addCommand(JPanel commandPanel);
	
	 private void m_jListOrdersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jListOrdersActionPerformed
	        SwingUtilities.invokeLater(new Runnable() {
	            public void run() {             
	                try {
	                    JSharedOrdersFinder orderFinder=JSharedOrdersFinder.getOrderFinder(AbstractJOrderProcessPanel.this, app, dlReceipts, dlSales, dlCustomers);
	                    orderFinder.setVisible(true);
	                    FindOrdersInfo findOrder=orderFinder.getSelectedOrder();
	                    if (findOrder != null) {
	                        setActiveOrder(findOrder.getId());
	                    }
	                } catch (BasicException e) {
	                    new MessageInf(e).show(AbstractJOrderProcessPanel.this);
	                }                    
	            }
	        }); 
	    }//GEN-LAST:event_m_jListOrdersActionPerformed
	 
	 private void setActiveOrder(String id) throws BasicException{
	        
	        // BEGIN TRANSACTION
	        Order order = dlReceipts.getSharedOrder(id);
	        if (order == null)  {
	            // Does not exists ???
	            throw new BasicException(AppLocal.getIntString("message.noOrder"));
	        } else {
	            //dlReceipts.setSharedOrderLocked(id,true,"system");
	            srcOrderView.setActiveOrder(order);   
	        } 
	        // END TRANSACTION                 
	    }
	 
	 private void insertShardOrder(Order newOrder){
	        if (newOrder!= null&&newOrder.getId()==null) {
	            try {
	            	String id=UUID.randomUUID().toString();
	            	newOrder.setID(id);
	                dlReceipts.insertSharedOrder(id, newOrder);
	            } catch (BasicException e) {
	                MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.storeShardOrderFailse"), e);
	                msg.show(this);
	                newOrder.setID(null);
	            }
	        }
	    }
	 
	 private void saveCurrentOrder(Order newOrder) {  
	        // save current Order, if exists,
	      if (newOrder != null&&newOrder.getId()!=null) {
	            try {
	                dlReceipts.updateSharedOrder(newOrder.getId(),newOrder,app.getAppUserView().getUser().getId());
	            } catch (BasicException e) {
	                MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.storeShardOrderFailse"), e);
	                 msg.show(this);
	            }
	        }
	    }
}
